package top.bluesword.laboratory;

import co.elastic.clients.transport.TransportUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;

/**
 * @author 李林峰
 */
public class RestClientBuilder {

  public static RestClient build() {
    BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
    basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "K8wWwbS2f4KtGGn5Fspe"));
    return RestClient
      .builder(HttpHost.create("https://localhost:9200"))
      .setHttpClientConfigCallback(httpClientBuilder ->
        httpClientBuilder
          .setDefaultCredentialsProvider(basicCredentialsProvider)
          .setSSLContext(TransportUtils.sslContextFromCaFingerprint("f1f345547362ca3c1e043d1da92d90cf87aaf8ba4748dc0d43ed34b4ba7ac775"))
      )
      .build();
  }

}
